package JTree;
import java.awt.EventQueue;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
import javax.swing.plaf.basic.BasicTreeUI;

public class U6  implements TreeModelListener{

	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					U6 window = new U6();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public U6() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 550, 400);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		JTree tree = new JTree();
		tree.setDragEnabled(true);
		
		
		tree.setEditable(true);
		tree.setShowsRootHandles(true);
		ImageIcon leafIcon = new ImageIcon("C://a.png");
		if (leafIcon != null) {
			DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
			renderer.setLeafIcon(leafIcon);
			tree.setCellRenderer(renderer);
		}

		MouseListener[] ms = tree.getMouseListeners();
		for (MouseListener l : ms) {
			System.out.println("l.getClass()=" + l.getClass());
		}

		tree.getSelectionModel().setSelectionMode(
				TreeSelectionModel.SINGLE_TREE_SELECTION);
		tree.addTreeSelectionListener(new TreeSelectionListener() {
			public void valueChanged(TreeSelectionEvent e) {
				System.out.println("haol");
			}
		});

		DefaultMutableTreeNode x = new DefaultMutableTreeNode("二大爷");
		DefaultMutableTreeNode user1=	new DefaultMutableTreeNode("user1");
		DefaultMutableTreeNode user1child=	new DefaultMutableTreeNode("user1child");
		user1.add(user1child);
		
		DefaultMutableTreeNode user2=	new DefaultMutableTreeNode("user2");
		DefaultMutableTreeNode user2child=	new DefaultMutableTreeNode("user2child");
		user2.add(user2child);
		DefaultMutableTreeNode user3=	new DefaultMutableTreeNode("user3");
		DefaultMutableTreeNode user3child=	new DefaultMutableTreeNode("user3child");
		user3.add(user3child);
		x.add(user1);
		x.add(user2);
		x.add(user3);

		DefaultTreeModel m = new DefaultTreeModel(x);

		tree.setModel(m);
		tree.setBounds(49, 24, 447, 286);
		frame.getContentPane().add(tree);
	}

	@Override
	public void treeNodesChanged(TreeModelEvent e) {
		// TODO Auto-generated method stub
		 DefaultMutableTreeNode node;
	        node = (DefaultMutableTreeNode)
	                 (e.getTreePath().getLastPathComponent());

	        /*
	         * If the event lists children, then the changed
	         * node is the child of the node we have already
	         * gotten.  Otherwise, the changed node and the
	         * specified node are the same.
	         */
	        try {
	            int index = e.getChildIndices()[0];
	            node = (DefaultMutableTreeNode)
	                   (node.getChildAt(index));
	        } catch (NullPointerException exc) {}

	        System.out.println("The user has finished editing the node.");
	        System.out.println("New value: " + node.getUserObject());

	}

	@Override
	public void treeNodesInserted(TreeModelEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void treeNodesRemoved(TreeModelEvent e) {
		// TODO Auto-generated method stub
		System.out.println("");
		
	}

	@Override
	public void treeStructureChanged(TreeModelEvent e) {
		// TODO Auto-generated method stub
		
	}
}
